package com.vtest.quartz.task.needlecard;

import com.vtest.quartz.domain.needle.BIDNeedleEntity;
import com.vtest.quartz.domain.needle.YOSNeedleEntity;
import com.vtest.quartz.service.needle.NeedleService;
import com.vtest.quartz.utils.MailHelper;
import com.vtest.quartz.utils.excel.ExcelUtil;
import com.vtest.quartz.utils.others.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.List;
import java.util.Locale;

/**
 * @program: Quartz
 * @author: tiger
 * @create: 2025-01-08 09:33
 * @Description : 针卡报表 - BID
 */

@Slf4j
@Component("BIDReport")
public class BIDReport {

    @Autowired
    private NeedleService needleService;

    public void sendNeedleReport() {
        log.debug("[BID 针卡报表] 任务开始执行");
        LocalDate date = LocalDate.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String time = date.format(dateTimeFormatter);

        List<BIDNeedleEntity> bidNeedleInfoList = needleService.findBIDNeedleInfo(time);

        LocalDate currentDate = LocalDate.now();
        // 使用WeekFields来定义一周的开始日
        WeekFields weekFields = WeekFields.of(Locale.getDefault());
        // 获取当前日期是一年中的第几周
        int weekNumber = currentDate.get(weekFields.weekOfYear());

        ExcelUtil<BIDNeedleEntity> util = new ExcelUtil<>(BIDNeedleEntity.class);

        //util.modifyTitle("pcs", "WW" + weekNumber);
        String[] strings = new String[61];
        for (int i = 1; i <= 60; i++) {
            if (i > weekNumber) strings[i - 1] = "pcs.w" + i;
        }
        strings[60] = "workFlag";
        util.hideColumn(strings);

        util.exportExcel(bidNeedleInfoList, "BID_Probe_Card_" + time, StringUtils.EMPTY, true);

        File localFile = new File(util.downFile);
        if (localFile.exists()) {
            MailHelper helper = new MailHelper();
            helper.sendMail("zhiqiang.song@v-test.com.cn;yawu.li@v-test.com.cn;huaiju.zhao@v-test.com.cn;VT_PCR@v-test.com.cn;summer.zhang@v-test.com.cn"
                    , "ruhu.wang@v-test.com.cn", "BID报表", "BID报表", util.downFile);
            //helper.sendMail("ruhu.wang@v-test.com.cn", "ruhu.wang@v-test.com.cn", "RCK报表", "RCK报表", file);
        }

        log.debug("[BID 针卡报表] 任务开始结束");
    }
}
